const Router = require("koa-router");
const router = new Router();
// const api = require("../mock");
const api = require("../database");
const captcha = require("svg-captcha");



router.get("/", async function (ctx) {
    ctx.render("login");
});
//验证码的生成
router.get("/codeImg", async function (ctx) {
    let codeObj = captcha.create({
        size: 4, // 验证码的长度,字符的个数
        ignoreChars: '0o1i', //验证码中排出0o1i
        noise: 4,// 干扰线的数量
        color: true, // 验证码的字符是否有颜色
        background: '#cc9966'
    });
    // console.log(codeObj.data);//图片地址
    console.log(codeObj.text);//验证码的文字
    ctx.session.code = codeObj.text;
    ctx.type = "svg";
    ctx.body = codeObj.data;
});



//get请求
router.get("/doLogin", async function (ctx) {
    // console.log(ctx.URL);//浏览器请求地址参数可以通过ctx.URL地址获取
    console.log(ctx.query);//ctx.query完成解析后的参数获取
    // if(ctx.query.username=="admin" && ctx.query.password=="123456"){
    //     await ctx.render("success");
    // }else{
    //     await ctx.redirect("/?msg=用户名或者密码错误");//重定向
    // }

    var u = {
        username: ctx.query.username,
        password: ctx.query.password
    };
    var user = api.loginFun(u);
    if (user) {
        await ctx.render("home", user);
    } else {
        await ctx.redirect("/?msg=用户名或者密码错误");//重定向
    }
});


// post请求，如果是post请求需要koa-body处理数据
// koa-body不仅能处理post请求，同时也能够处理文件类型的上传
router.post("/doLogin", async function (ctx) {
    console.log("请求体数据", ctx.request.body);
    if (ctx.request.body.code != ctx.session.code) {
        await ctx.redirect("/?msg=验证码错误");
        return;
    }

    var u = {
        username: ctx.request.body.username,
        password: ctx.request.body.password
    };
    var user = api.loginFun(u);
    if (user) {
        ctx.session.user = user;//将数据存储在session中
        await ctx.render("home", user);
    } else {
        await ctx.redirect("/?msg=用户名或者密码错误");//重定向
    }

});
2

router.get("/home", async function (ctx) {
    await ctx.render("home");

});

let authUser = async function (ctx, next) {
    if (ctx.session.user) {
        await next();
    } else {
        await ctx.redirect("/");
    }

};
router.get("/class", authUser, async function (ctx) {

    // let classObj = api.classObj;//获取班级数据
    // let classObj=api.loadClassList();//获取班级数据,对象
    // classObj.user = ctx.session.user;

    let classData = api.loadClassList();//数组
    let classObj = { list: classData };//转换成对象格式
    classObj.user = ctx.session.user;

    await ctx.render("class", classObj);
});

router.get("/student", authUser, async function (ctx) {
    // let stuObj = api.stuObj;//获取学生数据
    // let stuObj=api.loadStudentList();
    // console.log(stuObj);
    // stuObj.user = ctx.session.user;

    let stuData = api.loadStudentList();//数组
    console.log(stuData);
    let stuObj = { list: stuData };
    stuObj.user = ctx.session.user;
    await ctx.render("student", stuObj);
});
router.get("/objs", async function (ctx) {
    let objs = api.objs;//数组
    await ctx.render("obj", { "objs": objs });

});

router.get("/loginout", async function (ctx) {
    ctx.session.user = null;
    ctx.redirect("/");
});

router.get("/file", async function (ctx) {
    await ctx.render("file");
});

router.post("/upload", async function (ctx) {
    console.log(ctx.request.body);

    console.log(ctx.request.files);//获取上传的文件信息

    let filePath = ctx.request.files.headImg.filepath;//上传文件后的存储路径->服务器的磁盘物理路径
    let fileName = ctx.request.files.headImg.newFilename;//获取当前文件的名称

    console.log(ctx.origin);
    ctx.body = `

      
        <img src="${ctx.origin}/upload/${fileName}">
        <br>  
        <img src="/upload/${fileName}">
    `;

});

router.get("/addClass", async function (ctx) {
    ctx.render("addClass");
});
router.post("/addClass", async function (ctx) {
    let className = ctx.request.body.className;
    let result = api.insertClass(className);
    ctx.redirect("/class");
});

router.get("/updateClass", async function (ctx) {
    let id = ctx.query.id;//获取班级的id编号
    let classObj = api.findClassById(id);
    console.log(classObj);
    ctx.render("updateClass", classObj);
});

router.post("/updateClass", async function (ctx) {
    let classObj = ctx.request.body;
    api.updateClass(classObj);
    ctx.redirect("/class");
});
router.get("/deleteClass", async function (ctx) {
    let id = ctx.query.id;
    api.deleteClass(id);
    ctx.redirect("/class");
});

router.get("/addStudent", async function (ctx) {
    let classData = api.loadClassList();//查询所有的班级信息，数组
    ctx.render("addStudent", { list: classData });
});

router.post("/addStudent", async function (ctx) {
    let stu = ctx.request.body;
    let fileName = ctx.request.files.header.newFilename;
    stu.img = fileName;
    api.insertStudent(stu);
    ctx.redirect("/student");
});

router.get("/updateStudent", async function (ctx) {
    let id = ctx.query.id;
    let stu = api.findStuById(id);//根据id查询该学生的相关信息
    let classData = api.loadClassList();//找到所有班级数据
    let obj = {
        "stu": stu,
        "list": classData
    };
    ctx.render("updateStudent", obj);
});

router.post("/updateStudent", async function (ctx) {
    let stu = ctx.request.body;
    let filename = ctx.request.files.header.newFilename;
    stu.img = filename;
    console.log(stu);
    api.updateStudentById(stu);
    ctx.redirect("/student");
});

router.get("/deleteStudent", async function (ctx) {
    let id = ctx.query.id;
    api.deleteStudent(id);
    ctx.redirect("/student");
});
router.get("/register", async function (ctx) {
    ctx.render("register");
});
router.post("/register", async function (ctx) {
    let obj = ctx.request.body;
    console.log(obj)
    let row = api.isSameName(obj);
    console.log(row);
    if (row) {
        await ctx.redirect("/register?msg=该用户已存在");
    } else {
        api.insertUser(obj);
        await ctx.redirect("/")
    }
});


router.get(/\/.*/, async function (ctx) {
    await ctx.render("404");
});



module.exports = router;